package cn.yunyichina.provider.bone.service.impl;

import cn.yunyichina.provider.bone.dao.BoneDiagnoseAddressDao;
import cn.yunyichina.provider.bone.entity.BoneDiagnose;
import cn.yunyichina.provider.bone.entity.BoneDiagnoseAddress;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuxp on 2017/10/23.
 *
 * @param
 * @Descrition 面诊地址管理
 */
@Service("diagnoseaddressServiceImpl")
public class DiagnoseAddressServiceImpl {

    private static Logger logger = LoggerFactory.getLogger(DiagnoseAddressServiceImpl.class);

    private SimpleDateFormat simpleDateFormat  = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Autowired
    private BoneDiagnoseAddressDao boneDiagnoseAddressDao;

    /**
     * yyt.bone.diagnose.address.list
     * 查询地址列表
     * @param data
     * @return
     */
    public Response listDiagnoseAddress(String data){
        Response response = new Response();
        try {
            BoneDiagnoseAddress address = JSONObject.parseObject(data,BoneDiagnoseAddress.class);
            List<BoneDiagnoseAddress> addressList =boneDiagnoseAddressDao.select(address);
            response.setResultCodeAndMessageAndResult("0","查询地址列表成功！", JsonUtils.toJsonString(addressList));
        }catch (Exception e){
            response.setResultCodeAndMessage("-1","查询地址列表异常!");
            logger.error("获取地址列表异常!",e);
        }
        return  response;
    }

    /**
     * yyt.bone.diagnose.address.delete
     * 删除
     * @param data
     * @return
     */
    public Response deleteDiagnoseAddress(String data){
        Response response = new Response();
        try {
            BoneDiagnoseAddress request = JSONObject.parseObject(data,BoneDiagnoseAddress.class);
            BoneDiagnoseAddress address = boneDiagnoseAddressDao.selectById(request.getId());
            if(address == null){
                response.setResultCodeAndMessage("-2","地址不存在！");
                return response;
            }
            boneDiagnoseAddressDao.delete(address);
            response.setResultCodeAndMessage("0","删除地址成功！");
        }catch (Exception e){
            response.setResultCodeAndMessage("-1","删除地址异常!");
            logger.error("删除地址异常!",e);
        }
        return response;
    }

    /**
     * yyt.bone.diagnose.address.save
     * 保存
     * @param data
     * @return
     */
    public Response saveDiagnoseAddress(String data){
        Response response = new Response();
        try {
            BoneDiagnoseAddress address = JSONObject.parseObject(data,BoneDiagnoseAddress.class);
            if(StringUtils.isBlank(address.getId())){
                //新增
                address.setId(PKGenerator.generateId());
                address.setUpdateTime(simpleDateFormat.format(new Date()));
                address.setCreatedTime(simpleDateFormat.format(new Date()));
                boneDiagnoseAddressDao.insert(address);
                response.setResultCodeAndMessage("0","新增地址成功!");
            }else{
                BoneDiagnoseAddress entity = boneDiagnoseAddressDao.selectById(address.getId());
                if(entity == null){
                    response.setResultCodeAndMessage("-2","地址不存在!");
                    return  response;
                }
                address.setUpdateTime(simpleDateFormat.format(new Date()));
                //当更新内容为设置默认地址时  将原来默认地址状态改掉
                if(address.getIsDefault()==1){
                    BoneDiagnoseAddress defaultAddress = new BoneDiagnoseAddress();
                    defaultAddress.setIsDefault(1);
                    defaultAddress.setDoctorId(entity.getDoctorId());
                    List<BoneDiagnoseAddress> list = boneDiagnoseAddressDao.select(defaultAddress);
                    for(BoneDiagnoseAddress bda:list){
                        if(!bda.getId().equals(entity.getId())){
                            address.setDiagnoseAmount(bda.getDiagnoseAmount());
                            bda.setIsDefault(0);
                            boneDiagnoseAddressDao.update(bda);
                        }
                    }
                }
                boneDiagnoseAddressDao.update(address);
                response.setResultCodeAndMessage("0","修改地址成功!");
            }
        }catch (Exception e){
            response.setResultCodeAndMessage("-1","保存地址异常!");
            logger.error("保存地址异常!",e);
        }
        return  response;
    }
}
